<template>
  <div class="box">
    <div id="login_box">
      <h2 class="centered-text">LOGIN</h2>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" class="demo-ruleForm" :size="formSize" status-icon>
        <el-form-item label="用户名" prop="name" >
          <el-input v-model="ruleForm.name" clearable />
        </el-form-item>
        <el-form-item label="密码&nbsp&nbsp&nbsp" prop="pass" type="hidden">
          <el-input v-model="ruleForm.pass" show-password/>
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
            <el-button @click="resetForm(ruleFormRef)">注册</el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { useRouter } from "vue-router";
import { useUserStore } from '../stores/user'
import { onMounted, onUnmounted } from 'vue';
interface RuleForm {
  name: string;
  pass: String;
}
const router = useRouter();
const UserStore = useUserStore();
const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  name: "",
  pass: "",
});

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 10, message: "长度为3-10个字符", trigger: "blur" },
  ],
  pass: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 3, max: 10, message: "长度为3-10个字符", trigger: "blur" },
  ],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("表单验证通过!", ruleForm);
      const userData = {
        username: ruleForm.name,
        password: ruleForm.pass,
      };
      UserStore.getUserInfo(userData)
    } else {
      console.log("error submit!", fields);
    }
  });
};
onMounted(() => {
  document.body.style.backgroundImage = 'url("../assets/img/login.jpg")';
});

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));
</script>

<style scoped>

.box {
  background-image: url('../assets/img/login.jpg');
  background-size: cover;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
#login_box {
  width: 30%;
  max-width: 400px;
  margin: auto;
  margin-top: 10%;
  text-align: center;
  border-radius: 10px;
  padding: 20px 100px;
  background-color: rgba(0, 0, 0, 0.6);
}

.centered-text {
  justify-content: center;
  text-align: center;
}

h2 {
  color: #fff;
  margin-top: 5%;
  font-size: 24px;
}

.el-input {
  border: none;
  width: 100%;
  font-size: 15px;
  color: #fff;
  background: transparent;
  border-bottom: 2px solid #fff;
  padding: 0px 10px;
  padding-bottom: 10px;
  outline: none;
  margin-top: 10px;
}

.el-button {
  width: 150px;
  height: 40px;
  border-radius: 20px;
  border: none;
  color: #fff;
  text-align: center;
  line-height: 40px;
  font-size: 16px;
  margin: 0 50px;
  background-image: linear-gradient(to right, #30cfd0, #330867);
}

.el-button-group {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
}



a {
  color: #b94648;
}
</style>
